package com.atguigu.tingshu.common.login;

import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserInfo;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Component
@Aspect
public class TsLoginAspect {
    @Autowired
    private RedisTemplate redisTemplate;

    @Around("@annotation(tsLogin)")
    public Object doBasicProfiling(ProceedingJoinPoint point, TsLogin tsLogin) throws Throwable {
        /*
        1.先判断当前用户是否登录，如果登录成功 将token放到请求头中
                判断请求头中是否有token就可以了
        2.如果有token
                需要查询缓存中是否真正存在用户信息
         */
        RequestAttributes previousAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) previousAttributes;
        HttpServletRequest request = servletRequestAttributes.getRequest();
//        HttpServletResponse response = servletRequestAttributes.getResponse();
        String token = request.getHeader("token");
        //判断注解的属性
        if (tsLogin.required()) {
            //判断注解是否为空
            if(StringUtils.isEmpty(token)){
                //说明没有登陆过;    LOGIN_AUTH(208, "未登陆"),
                throw new GuiguException(ResultCodeEnum.LOGIN_AUTH);
            }
            // 判断缓存中是哦否真正有用户信息;redis
            String userLoginKey = RedisConstant.USER_LOGIN_KEY_PREFIX + token;
            // 获取缓存中的数据
            UserInfo userInfo = (UserInfo) this.redisTemplate.opsForValue().get(userLoginKey);
            // 如果用户信息为空
            if(null == userInfo){
                //说明没有登录郭  LOGIN_AUTH(208, "未登陆"),
                throw new GuiguException(ResultCodeEnum.LOGIN_AUTH);
            }
        }
        //判断当前token不为空
        if (!StringUtils.isEmpty(token)) {
            //有token 那么缓存中有可能 存在
            String userLoginKey = RedisConstant.USER_LOGIN_KEY_PREFIX + token;
            //获取缓存中数据
            UserInfo userInfo = (UserInfo) this.redisTemplate.opsForValue().get(userLoginKey);
            //判断用户对象
            if(null != userInfo){
                // 将用户id存储到本地
                AuthContextHolder.setUserId(userInfo.getId());
            }
        }
        //声明一个对象
        Object proceed;
        try {
            //  执行业务方法
            proceed = point.proceed();
        } finally {
            //  防止内存泄露
            AuthContextHolder.removeUserId();
        }
        return proceed;
    }


}


